var http = require("../../../utils/https.js")
Page({
  data: {
    // 密码相关数据
    oldPassword: '',
    newPassword: '',
    confirmPassword: '',
    
    // 密码可见性控制
    oldPwdVisible: true,
    newPwdVisible: true,
    confirmPwdVisible: true,
    
    // 密码输入框类型（text/password）
    oldPwdType: 'password',
    newPwdType: 'password',
    confirmPwdType: 'password',
    
    // 错误提示信息
    oldPasswordError: '',
    newPasswordError: '',
    confirmPasswordError: '',
    
    // 提交状态控制
    canSubmit: false,
    isSubmitting: false,
    
    // 成功提示
    showSuccessToast: false
  },
  
  // 切换旧密码可见性
  toggleOldPwdVisible() {
    this.setData({
      oldPwdVisible: !this.data.oldPwdVisible,
      oldPwdType: this.data.oldPwdVisible ? 'password' : 'text'
    });
  },
  
  // 切换新密码可见性
  toggleNewPwdVisible() {
    this.setData({
      newPwdVisible: !this.data.newPwdVisible,
      newPwdType: this.data.newPwdVisible ? 'password' : 'text'
    });
  },
  
  // 切换确认密码可见性
  toggleConfirmPwdVisible() {
    this.setData({
      confirmPwdVisible: !this.data.confirmPwdVisible,
      confirmPwdType: this.data.confirmPwdVisible ? 'password' : 'text'
    });
  },
  
  // 处理旧密码输入
  handleOldPasswordInput(e) {
    const oldPassword = e.detail.value;
    this.setData({ oldPassword });
    this.validateForm();
  },
  
  // 处理新密码输入
  handleNewPasswordInput(e) {
    const newPassword = e.detail.value;
    this.setData({ newPassword });
    this.validateForm();
  },
  
  // 处理确认密码输入
  handleConfirmPasswordInput(e) {
    const confirmPassword = e.detail.value;
    this.setData({ confirmPassword });
    this.validateForm();
  },
  
  // 表单验证
  validateForm() {
    const { oldPassword, newPassword, confirmPassword } = this.data;
    let oldPasswordError = '';
    let newPasswordError = '';
    let confirmPasswordError = '';
    let canSubmit = true;
    
    // 验证旧密码
    if (!oldPassword) {
      oldPasswordError = '请输入旧密码';
      canSubmit = false;
    }
    
    // 验证新密码
    if (!newPassword) {
      newPasswordError = '请输入新密码';
      canSubmit = false;
    } else if (newPassword.length < 6 || newPassword.length > 16) {
      newPasswordError = '密码长度必须为6-16位';
      canSubmit = false;
    } else if (newPassword === oldPassword) {
      newPasswordError = '新密码不能与旧密码相同';
      canSubmit = false;
    }
    
    // 验证确认密码
    if (!confirmPassword) {
      confirmPasswordError = '请确认新密码';
      canSubmit = false;
    } else if (confirmPassword !== newPassword) {
      confirmPasswordError = '两次输入的密码不一致';
      canSubmit = false;
    }
    
    // 更新状态
    this.setData({
      oldPasswordError,
      newPasswordError,
      confirmPasswordError,
      canSubmit
    });
  },
  
  // 提交表单
  submitForm() {
    // 再次验证表单
    this.validateForm();
    
    if (!this.data.canSubmit) {
      return;
    }
    console.log(this.data.newPassword);
    var userInfo = wx.getStorageSync('userInfo')
    if(userInfo==null||userInfo==undefined||userInfo.userId==null||userInfo.userId==undefined){
      wx.showToast({
        title: '登录过期',
        icon:'none',
        duration:1000
      })
      setTimeout(()=>{
        wx.redirectTo({
          url: '/index/pages/mzhLogin/mzhLogin',
        })
      },1500)
    }
    var data = {
      userId:userInfo.userId,
      oldPassword:this.data.oldPassword,
      newPassword:this.data.newPassword
    }
    console.log(data);
    var that = this

    http.PUT('/system/vip_point/updatePassword',data, function(res) {
      console.log('成功:', res);
      if(res.code===200){
        wx.showToast({
          title: '修改成功',
          icon:'none',
          duration:1000
        })
        setTimeout(()=>{
          wx.navigateBack()
        },1500)
      }else{
        that.setData({
          oldPassword: '',
          newPassword: '',
          confirmPassword: '',
        })
        wx.showToast({
          title: res.msg,
          duration: 1000,
          icon: 'none'
        })
      }
    }, function(err) {
      console.error('失败:', err);
    });
  }
});
